<?php
namespace Kuoqi\Interpreter;
class Fors{
	static protected $assign,$code;
	static public function execCode($code,$assign = array()){
		self::$assign = $assign;
		self::$code = $code;
		self::__initializate();
		$code = self::$code;
		self::$code = '';
		return 	$code;	
	}
	static public function __initializate(){
		$data = self::$code;
		preg_match_all(self::need(),$data,$arr);
		foreach($arr[0] as $k=>$v){
			$data = str_replace($v,self::replace($arr[1][$k]),$data);
		}
		$data=str_replace(self::needend(),self::replacend(),$data);
		self::$code = $data;
	}
	static private function need(){
		return '/\<for\s+condition\=[\'\"]([\&\%\w\d\,\[\]\'\"\_\<\>\=\+\-\*\/\$\!\(\)\;]+)[\'\"]\s*\>/';
	}
	static private function needend(){
		return '</for>';
	}
	static private function replace($val){
		return '<?php for('.$val.'):?>';
	}
	static private function replacend(){
		return '<?php endfor;?>';
	}
}